package com.lovo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j(topic = "test")
public class LogAspect {
    @Pointcut("execution(* com.lovo.service..find*(..))")
    public void logPointcut(){}

    @Before("logPointcut()")
    public void sayHello(JoinPoint joinPoint){
        log.debug("Hello："+joinPoint.getSignature());
    }
    @After("logPointcut()")
    public void sayBye(JoinPoint joinPoint){
//        System.out.println("Bye："+joinPoint.getSignature());
        log.debug("bye："+joinPoint.getSignature());
    }
    @AfterThrowing("logPointcut()")
    public void sayError(JoinPoint joinPoint){
//        System.out.println("Error："+joinPoint.getSignature());
        log.error("Error："+joinPoint.getSignature());
    }
    @Around("execution(* com.lovo..*(..))")
    public Object sayAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.debug("进入方法："+joinPoint.getSignature());
        Object obj = null;
        try {
            obj = joinPoint.proceed(joinPoint.getArgs());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            log.error("抛出异常："+joinPoint.getSignature());
            throw throwable;
        }
        log.debug("退出方法："+joinPoint.getSignature());
        return obj;
    }
}
